package com.ruoyi.weixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.weixin.entity.*;
import com.ruoyi.weixin.mapper.FrentsMapper;
import com.ruoyi.weixin.service.ArticleService;
import com.ruoyi.weixin.service.FrentsService;
import com.ruoyi.weixin.service.GoodsService;
import com.ruoyi.weixin.service.PicUrlService;
import com.ruoyi.weixin.untils.OssUtil;
import com.ruoyi.weixin.vo.userId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * (Frents)表服务实现类
 *
 * @author makejava
 * @since 2023-12-23 09:56:40
 */
@Service("frentsService")
public class FrentsServiceImpl extends ServiceImpl<FrentsMapper, Frents> implements FrentsService {

    @Autowired
    PicUrlService picUrlService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    ArticleService articleService;

    @Override
    public R<?> AllList(userId pagesResultp) {
        Page<Frents> frentsPage = new Page<>(pagesResultp.getPageNum(), pagesResultp.getPageSize());
        Page<Frents> page = super.page(frentsPage, new LambdaQueryWrapper<Frents>().eq(Frents::getStatus, 1).orderByDesc(Frents::getCreatTime));
        List<Frents> list = page.getRecords();
        List<PicUrl> urls;
        List<Goods> goods;
        for (Frents frents : list) {
            urls = picUrlService.list(new LambdaQueryWrapper<PicUrl>().eq(PicUrl::getFrentsId, frents.getId()).orderByAsc(PicUrl::getId));
            frents.setUrls(urls.stream().map(item -> item.getPicUrl()).collect(Collectors.toList()));
            goods = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getFrentsId, frents.getId()).orderByAsc(Goods::getId));
            List<String> collect = goods.stream().map(item -> item.getUserName()).collect(Collectors.toList());
            for (Goods good : goods) {
                if (good.getUserId().equals(pagesResultp.getUserId())){
                    frents.setColorAble("1");
                }else {
                    frents.setColorAble("2");
                }
            }
            if (collect.size() >= 2) {
                collect.subList(0, 2);
            }
            frents.setGoods(collect);
            frents.setArticleCount(articleService.count(new LambdaQueryWrapper<Article>().eq(Article::getPreId, frents.getId()).eq(Article::getType,0).isNull(Article::getParentId)));
            frents.setGoodsCount(goods.size());
        }
        page.setRecords(list);
        return R.ok(page);
    }

    @Override
    public R<?> listbyid(String id,String userId) {
        Frents frents = super.getById(id);
        List<PicUrl> urls = picUrlService.list(new LambdaQueryWrapper<PicUrl>().eq(PicUrl::getFrentsId, frents.getId()));
        frents.setUrls(urls.stream().map(item -> item.getPicUrl()).collect(Collectors.toList()));
        List<Goods> goods = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getFrentsId, frents.getId()));
        for (Goods good : goods) {
            if (good.getUserId().equals(Integer.parseInt(userId))){
                frents.setColorAble("1");
            }else {
                frents.setColorAble("2");
            }
        }
        frents.setGoods(goods.stream().map(item -> item.getUserName()).collect(Collectors.toList()));
        frents.setArticleCount(articleService.count(new LambdaQueryWrapper<Article>().eq(Article::getPreId, frents.getId()).eq(Article::getType,0).isNull(Article::getParentId)));
        frents.setGoodsCount(goods.size());

        List<Article> list = articleService.list(new LambdaQueryWrapper<Article>().eq(Article::getPreId,id).eq(Article::getType,0).isNull(Article::getParentId));
        List<Article> articles = listfindChild(list);

        frents.setArticleList(articles);
        return R.ok(frents);
    }

    @Override
    public R<?> listSi(userId userId) {
        Page<Frents> frentsPage = new Page<>(userId.getPageNum(), userId.getPageSize());
        Page<Frents> page = super.page(frentsPage, new LambdaQueryWrapper<Frents>().orderByDesc(Frents::getCreatTime).eq(Frents::getUserId, userId.getUserId()));
        List<Frents> list = page.getRecords();
        List<PicUrl> urls;
        List<Goods> goods;
        for (Frents frents :list) {
            urls = picUrlService.list(new LambdaQueryWrapper<PicUrl>().eq(PicUrl::getFrentsId, frents.getId()).orderByAsc(PicUrl::getId));
            frents.setUrls(urls.stream().map(item -> item.getPicUrl()).collect(Collectors.toList()));
            goods = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getFrentsId, frents.getId()).orderByAsc(Goods::getId));
            List<String> collect = goods.stream().map(item -> item.getUserName()).collect(Collectors.toList());
            for (Goods good : goods) {
                if (good.getUserId().equals(userId.getUserId())){
                    frents.setColorAble("1");
                }else {
                    frents.setColorAble("2");
                }
            }
            if (collect.size() >= 2) {
                collect.subList(0, 2);
            }
            frents.setGoods(collect);
            frents.setArticleCount(articleService.count(new LambdaQueryWrapper<Article>().eq(Article::getPreId, frents.getId()).eq(Article::getType,0).isNull(Article::getParentId)));
            frents.setGoodsCount(goods.size());

            frentsPage.setRecords(list);
        }
        return R.ok(page);
    }

    @Override
    @Transactional
    public int delete(String userId, String frentId) {
        super.removeById(frentId);
        goodsService.remove(new LambdaQueryWrapper<Goods>().eq(Goods::getFrentsId, frentId).eq(Goods::getUserId, userId));
        LambdaQueryWrapper<PicUrl> eq = new LambdaQueryWrapper<PicUrl>().eq(PicUrl::getFrentsId, frentId).eq(PicUrl::getUserId, userId);
        List<PicUrl> list = picUrlService.list(eq);
        String result=null;
        for (PicUrl picUrl : list) {
            Pattern pattern = Pattern.compile("com/(.*)");
            Matcher matcher = pattern.matcher(picUrl.getPicUrl());
            if (matcher.find()) {
                 result = matcher.group(1);
                System.out.println(result);
            } else {
                System.out.println("No match found");
            }
            OssUtil.delete(result);
        }
        picUrlService.remove(eq);
        return 0;
    }

    private List<Article> listfindChild(List<Article> list) {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Article> list1;
        for (Article article : list) {
            articleLambdaQueryWrapper.clear();
            list1 = articleService.list(articleLambdaQueryWrapper.eq(Article::getParentId, article.getId()));
            if (!list1.isEmpty()) {
                article.setChildren(list1);
                listfindChild(list1);
            }
        }
        return list;
    }


}
